<?php

require_once("include/bittorrent.php");
dbconn();
loggedinorreturn();
parked();
$gift = $_GET['r'];
$deltime = time() - 86400;
$res = mysql_fetch_array(sql_query("SELECT * FROM gift WHERE cards = '$gift'"));
if (!$res) {
	stderr("错误", "不存在的红包！");
} elseif ($deltime - strtotime($res['date']) >= 0) {
	stderr("错误", "过期的红包！");
}
stdhead("魔力红包");
$num = mysql_num_rows(sql_query("SELECT id FROM giftlog WHERE cards = '$gift'"));
$res1 = mysql_fetch_array(sql_query("SELECT SUM(bonus) AS bonus FROM giftlog WHERE cards = '$gift'")); //统计该红包所有已经被领取的魔力值
$getnum = $res['num'] - $num; //计算剩余名额
if ($res['num'] == $res['bonus']) {//领取总人数等于红包金额，则每人只能领取1个魔力值
	$getbonus = 1;
} else {
	$max = $res['bonus'] - $res1['bonus']; //统计该红包剩余魔力值
	$safe = $max / $getnum * 2;
	if ($max < $safe) {
		$getbonus = $max;
	} else {
		$getbonus = mt_rand(1, $safe);
	}
}
$res2 = sql_query("SELECT * FROM giftlog WHERE userid = " . $CURUSER['id'] . " AND cards = '$gift'");
if (mysql_num_rows($res2) > 0) {
	$HTMLOUT .="<h1 align='center'>红包统计</h1>";
	$res = sql_query("SELECT * FROM giftlog WHERE cards = '$gift' ORDER BY id DESC"); //降序排列，最新生成的最先显示
	$HTMLOUT .= bjtable_gift($res, "所有红包");
	$HTMLOUT .="<br /><br />";
	print $HTMLOUT;
	/*
	  echo "<script>alert('您已经领取过该红包！但是您可以发个红包。');location.href = 'giftmanager.php';</script>";
	  die();
	 *
	 */
} else {
	$getname = get_user_name_from_id($res['userid']);
	if ($res['bonus'] <= $res1['bonus']) {
		echo "<script>alert('来晚了，$getname 的红包已经被领取完了！不过您可以发个红包让大家乐呵乐呵~');location.href = 'giftmanager.php';</script>";
		die();
	} else {
		sql_query("UPDATE users SET seedbonus = seedbonus + $getbonus WHERE id = $CURUSER[id]");
		sql_query("INSERT INTO giftlog (userid, cards, bonus, time) VALUE ($CURUSER[id], '$gift', $getbonus, '" . date("Y-m-d H:i:s", time()) . "')");
		writeBonusComment($CURUSER['id'], "领取 $getname 的红包获得了 $getbonus 个魔力值");
		echo "<script>alert('成功领取了 $getname 的红包，获得 $getbonus 个魔力值！');location.href = 'gift.php?r=$gift';</script>";
	}
}

function begin_table_gift($fullwidth = false, $padding = 5) {
	$width = "";
	if ($fullwidth)
		$width .= " width=50%";
	return("<table class='main" . $width . "' border='1' cellspacing='0' cellpadding='" . $padding . "'>");
}

function end_table_gift() {
	return("</table>");
}

function begin_frame_gift($caption = "", $center = false, $padding = 5, $width = "100%", $caption_center = "left") {
	$tdextra = "";
	if ($center)
		$tdextra .= " align='center'";
	return(($caption ? "<h2 align='" . $caption_center . "'>" . $caption . "</h2>" : "") . "<table width='" . $width . "' border='1' cellspacing='0' cellpadding='" . $padding . "'>" . "<tr><td class='text' $tdextra>");
}

function end_frame_gift() {
	return("</td></tr></table>");
}

function bjtable_gift($res, $frame_caption) {
	global $gift;
	$htmlout = '';
	$htmlout .= begin_frame_gift($frame_caption, true);
	$htmlout .= begin_table_gift();
	$htmlout .="<tr><td class='colhead'>序号</td><td class='colhead' align='center'>用户</td><td class='colhead' align='center'>数量</td><td class='colhead' align='center'>领取时间</td></tr>";
	$num = 0;
	$best = mysql_fetch_array(sql_query("SELECT bonus FROM giftlog WHERE cards = '$gift' ORDER BY bonus DESC"));
	while ($a = mysql_fetch_assoc($res)) {
		++$num;
		$htmlout .= ($a['bonus'] == $best['bonus'] ? "<tr class='torrent_table seeders'>" : "<tr class='torrent_table'>") .
				"<td>$num</td>" .
				"<td align='center'><a href='userdetails.php?id=$a[userid]' target='_blank' class='" . get_user_class_name_from_userid($a['userid']) . "_Name'><b>" . get_user_name_from_id($a['userid']) . "</b></a>" . ($a['bonus'] == $best['bonus'] ? "(运气王)" : "") . "</td>" .
				"<td align='center'>" . round($a['bonus']) . "</td>" .
				"<td align='center'>" . $a['time'] . "</td>" .
				"</tr>";
	}
	$htmlout .= end_table_gift();
	$htmlout .= end_frame_gift();
	return $htmlout;
}

stdfoot();
